package com.seefly.firstmall.controller;

import java.io.UnsupportedEncodingException;
import java.text.DateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.seefly.firstmall.service.IBrandService;
import com.seefly.firstmall.util.AppConfigParam;

/**
 * Handles requests for the application home page.
 */
@Controller
public class HomeController {
	
	private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
	
	@Autowired
	private IBrandService brandService;
	
	/**
	 * Simply selects the home view to render by returning its name.
	 * @throws UnsupportedEncodingException 
	 */
	@RequestMapping(value = "/index", method = RequestMethod.GET)
	public String index(Locale locale, Model model) throws UnsupportedEncodingException {
		
		
		Date date = new Date();
		DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);
		
		String formattedDate = dateFormat.format(date);
		
		logger.info("Welcome home! The client locale is {}, and servertime is {}", locale,formattedDate);
		
		model.addAttribute("serverTime", formattedDate );
		model.addAttribute("WebAppName", AppConfigParam.appConfig.getWebAppName());
		model.addAttribute("carousels", brandService.getAllCarousel());
		model.addAttribute("brands",brandService.getAllBrand());
		
		
		return "home/home";
		
	}
	
	@RequestMapping(value = "/", method = RequestMethod.GET)
	public String home(Locale locale, Model model) throws UnsupportedEncodingException {
		
		return "redirect:/index";
		
	}
	
}
